<?php

class Application_Model_SoaMapper {
	
	protected $_connection;
	
	public function __construct() {
		
		$this->_connection = new Application_Model_DbTable_SoaCampaign();
	}
	
	public function getCustomerById($partyNumber) {
		
		try {
			$result = $this->_connection->fetchRow('partyNumber='.$partyNumber)->toArray();
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		
		
		return $result;
	}
	
	public function getLastId() {
		try {
			$result = $this->_connection->getAdapter()->lastInsertId('bis.tbl_soaCampaign', 'id');
		} catch (Exception $e) {
			echo $e->getMessage();
		}
	} 

	public function add($soaCampaign) {
		
		$soaArray = array('campaignDesc' => $soaCampaign->getCampaignDesc(),
						'customerNumber' => $soaCampaign->getCustomerNumber(),
						'budgetCategory' => $soaCampaign->getBudgetCategory(),
						'createdOn' => $soaCampaign->getCreatedOn(),
						'createdBy' => $soaCampaign->getCreatedBy(),
						'startDate' => $soaCampaign->getStartDate(),
						'endDate' => $soaCampaign->getEndDate(),
						'status' => $soaCampaign->getStatus());
		
		try {
			$this->_connection->insert($soaArray);
 		} catch(Exception $e) {
 			echo $e->getMessage();
 		} 		
	}
	
	public function getSoaCampaignById($campaign_id)
	{
		$result = $this->_connection->fetchRow('id='.$campaign_id);
		
		$soa = new Application_Model_Soa();
		
		//fill and return soa campaign
		$soa->setId($result->id);
		$soa->setBudgetCategory($result->budgetCategory);
		$soa->setCampaignDesc($result->campaignDesc);
		$soa->setCreatedBy($result->createdBy);
		$soa->setCreatedOn($result->createdOn);
		$soa->setCustomerNumber($result->customerNumber);
		$soa->setEndDate($result->enddate);
		$soa->setStartDate($result->startdate);
		$soa->setStatus($result->status);
		
		return $soa;
	}
	
	public function getSoaItems($campaign_id)
	{
		$table = new Application_Model_DbTable_SoaCampaignItem();
		
		$select = $table->select()
						->from('tbl_soaCampaignItem')
						->where('campaign_id='.$campaign_id);
		
		$rows = $table->fetchAll($select)->toArray();
		
		return $rows;
	}
	
 	public function addSoaItem(Application_Model_SoaItem $soaItem) {
 		$data = array('campaign_id' => $soaItem->getParentId(),
 							'inventoryItem' => $soaItem->getInventoryItem(),
 							'price' => $soaItem->getPrice(),
 							'planQty' => $soaItem->getPlanQty());
 		
 		$table = new Application_Model_DbTable_SoaCampaignItem();
 		$table->insert($data);
 	}
 	
	public function deleteItem($itemId) {
		$table = new Application_Model_DbTable_SoaCampaignItem();
		
		$table->delete('id='.$itemId);
	} 

	
	public function getViewInfo($invItem, $customerNbr) {
		 
		$inventoryDb = new Application_Model_DbTable_InventoryItem();
		$item = $inventoryDb->getInventoryItem($invItem);
		 
		$channel = Zend_Db_Table::getDefaultAdapter()->fetchRow('
    			SELECT qtyCustomerInventory FROM bis.tbl_sellthrough
    				WHERE customerNumber=\''.$customerNbr.'\'
    				AND inventoryItem=\''.$invItem.'\'
    				AND yearWeekNbr=(SELECT MAX(yearWeekNbr) from bis.tbl_sellthrough)');
		
		$st_wk1 = Zend_Db_Table::getDefaultAdapter()->fetchRow('
    			SELECT qtySellthrough AS week1 FROM bis.tbl_sellthrough
    				WHERE customerNumber=\''.$customerNbr.'\'
    				AND inventoryItem=\''.$invItem.'\'
    				AND weekEndDate=(SELECT MAX(weekEndDate) from bis.tbl_sellthrough)');
		
		$st_wk2 = Zend_Db_Table::getDefaultAdapter()->fetchRow('
    			SELECT qtySellthrough AS week2 FROM bis.tbl_sellthrough
    				WHERE customerNumber=\''.$customerNbr.'\'
    				AND inventoryItem=\''.$invItem.'\'
    				AND weekEndDate=(SELECT MAX(weekEndDate)- interval 1 week from bis.tbl_sellthrough)');

		$st_wk3 = Zend_Db_Table::getDefaultAdapter()->fetchRow('
    			SELECT qtySellthrough AS week3 FROM bis.tbl_sellthrough
    				WHERE customerNumber=\''.$customerNbr.'\'
    				AND inventoryItem=\''.$invItem.'\'
    				AND weekEndDate=(SELECT MAX(weekEndDate)- interval 2 week from bis.tbl_sellthrough)');
		 
		//sellthrough + inventoryInfo
		$data = array_merge($channel, $item, $st_wk1, $st_wk2, $st_wk3);


		
		return $data;
	}
	
}